<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-card">
        <!-- 头部Logo -->
        <div class="login-header">
          <div class="logo">
            <div class="logo-icon">🛡️</div>
            <h1 class="logo-title">管理控制台</h1>
            <p class="logo-subtitle">农产品销售平台管理系统</p>
          </div>
        </div>

        <!-- 登录表单 -->
        <div class="login-form-container">
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            size="large"
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                prefix-icon="User"
                clearable
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                clearable
                @keyup.enter="handleLogin"
              />
            </el-form-item>

            <el-form-item prop="userType">
              <el-select
                v-model="loginForm.userType"
                placeholder="请选择用户类型"
                style="width: 100%"
              >
                <el-option label="管理员" value="users">
                  <span>👨‍💼 管理员</span>
                </el-option>
                <el-option label="商家" value="shangjia">
                  <span>🏪 商家</span>
                </el-option>
                <el-option label="用户" value="yonghu">
                  <span>👤 用户</span>
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                class="login-btn"
                :loading="loading"
                @click="handleLogin"
                size="large"
              >
                {{ loading ? '登录中...' : '立即登录' }}
              </el-button>
            </el-form-item>
          </el-form>

          <!-- 底部链接 -->
          <div class="login-footer">
            <div class="register-link">
              <el-link type="primary" @click="goToRegister">还没有账号？立即注册</el-link>
            </div>

            <div class="quick-links">
              <a href="http://localhost:3000" target="_blank" class="quick-link">
                用户端
              </a>
              <span class="divider">|</span>
              <a href="http://localhost:8082" target="_blank" class="quick-link">
                商家端
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const { proxy } = getCurrentInstance()
const http = proxy.$http

// 响应式数据
const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  userType: 'users'
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  userType: [
    { required: true, message: '请选择用户类型', trigger: 'change' }
  ]
}

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    console.log('登录数据:', loginForm)

    // 调用实际的登录API
    const formData = new FormData()
    formData.append('username', loginForm.username)
    formData.append('password', loginForm.password)

    const result = await http({
      url: `${loginForm.userType}/login`,
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })

    console.log('登录响应:', result)

    if (result.code === 0) {
      const { token, username, userId, role } = result
      const userInfo = {
        id: userId,
        username: loginForm.username,
        name: username,
        userType: loginForm.userType,
        role: role,
        avatar: ''
      }

      // 保存登录信息
      localStorage.setItem('token', token)
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      localStorage.setItem('loginTable', loginForm.userType)

      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error(result.msg || '登录失败')
    }
    
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 2rem;
}

.login-container {
  width: 100%;
  max-width: 450px;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  overflow: hidden;
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 登录头部 */
.login-header {
  text-align: center;
  padding: 3rem 2rem 2rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.logo-title {
  font-size: 1.8rem;
  font-weight: bold;
  margin-bottom: 0.5rem;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}

.logo-subtitle {
  font-size: 0.9rem;
  opacity: 0.9;
  margin: 0;
}

/* 表单容器 */
.login-form-container {
  padding: 2rem;
}

.login-form {
  margin-bottom: 1.5rem;
}

.login-form :deep(.el-form-item) {
  margin-bottom: 1.2rem;
}

.login-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.login-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.login-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

.login-btn {
  width: 100%;
  height: 44px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.login-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.3);
}

/* 底部 */
.login-footer {
  text-align: center;
  padding-top: 1rem;
  border-top: 1px solid #eee;
}

.register-link {
  margin-bottom: 1rem;
}

.quick-links {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.quick-link {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.quick-link:hover {
  color: #5a67d8;
}

.divider {
  color: #ccc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-page {
    padding: 1rem;
  }

  .login-container {
    max-width: 100%;
  }

  .login-header {
    padding: 2rem 1.5rem 1.5rem;
  }

  .logo-title {
    font-size: 1.5rem;
  }

  .logo-icon {
    font-size: 2.5rem;
  }

  .login-form-container {
    padding: 1.5rem;
  }

  .quick-links {
    flex-direction: column;
    gap: 0.75rem;
  }
}

@media (max-width: 480px) {
  .login-page {
    padding: 0.5rem;
  }

  .login-header {
    padding: 1.5rem 1rem 1rem;
  }

  .logo-title {
    font-size: 1.3rem;
  }

  .login-form-container {
    padding: 1rem;
  }
}
</style>
